package com.toe.forum.webapi.controller;


import com.toe.common.restful.JsonPage;
import com.toe.common.restful.JsonResult;
import com.toe.forum.service.ForumCategoryService;
import com.toe.pojo.dto.forum.CategoryAddDTO;
import com.toe.pojo.dto.forum.CategoryUpdateDTO;
import com.toe.pojo.vo.forum.CategoryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文章分类表 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/forum-category")
@Api(tags = "论坛文章分类模块")
public class ForumCategoryController {

    @Autowired
    private ForumCategoryService forumCategoryService;

    @ApiOperation("添加文章分类")
    @PostMapping("/add")
    public JsonResult<Void> addCategory(@RequestBody @Validated CategoryAddDTO categoryDTO){
        forumCategoryService.addCategory(categoryDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除文章分类")
    @PostMapping("/delete/{id}")
    public JsonResult<Void> deleteCategory(@PathVariable("id") Long id){
        forumCategoryService.deleteCategoryById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改文章分类")
    @PostMapping("/update")
    public JsonResult<Void> updateCategory(@Validated CategoryUpdateDTO categoryUpdateDTO){
        forumCategoryService.updateCategoryById(categoryUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询所有文章分类")
    @GetMapping("/select/list")
    public JsonResult<List<CategoryVO>> selectCategory(){
        List<CategoryVO> categoryVOS = forumCategoryService.selectCategoryList();
        return JsonResult.ok(categoryVOS);
    }

    @ApiOperation("分页查询所有的文章分类")
    @GetMapping("/select/pageList")
    public JsonResult<JsonPage<CategoryVO>> selectCategoryPage(Integer pageNum,Integer pageSize){
        JsonPage<CategoryVO> categoryVOJsonPage = forumCategoryService.selectCategoryList(pageNum, pageSize);
        return JsonResult.ok(categoryVOJsonPage);
    }

    @ApiOperation("查询对应id的文章分类")
    @GetMapping("/select/{id}")
    public JsonResult<CategoryVO> selectCategoryById(@PathVariable long id){
        CategoryVO categoryVO = forumCategoryService.selectCategoryById(id);
        return JsonResult.ok(categoryVO);
    }
}

